#!usr/bin/env python
#-*- coding:utf-8 -*-
"""
数据源：巨潮 -> 市场资讯 -> 上市公司 -> 公司资讯 -> 高管人员
爬取类目：高管人员
股票类型不包括港股（港股没有最新资料）
url_sample: http://www.cninfo.com.cn/information/management/szmb000001.html
"""
"""
result_sample_of_company_management = {
    '_id': '',
    'stockCode': '',
    'companyShortName': '',
    'stockType': '',
    'management':[{
        'managerName': '',
        'managerPosition': '',
        'managerBirthYear': '',
        'managerGender': '',
        'managerDegree': '',
    },],
}
"""


import sys

from company_formaltable_crawler import CompanyFormalTable
from company_crawler import CompanyInfoUtil
from xtls.logger import get_logger
from xtls.timeparser import now

reload(sys)
sys.setdefaultencoding('utf-8')

CATEGORY = 'management'

DETAIL_MAPPING = {
    u'姓名': 'managerName',
    u'职务': 'managerPosition',
    u'出生年份': 'managerBirthYear',
    u'性别': 'managerGender',
    u'学历'	: 'managerDegree',
}

class CompanyManagement(CompanyFormalTable):
    def __init__(self, company_info_util_instance):
        super(CompanyManagement, self).__init__(company_info_util_instance=company_info_util_instance)

    def save(self, data):
        data['updateTime'] = now()
        for key, value in data.items():
            if key in ['managementDetail']:
                for dividend_list in value:
                    for child_key, child_value in dividend_list.items():
                        print '{}: {}'.format(child_key, child_value)
                print '-'*40
            else:
                print '{}: {}'.format(key, value)

        print "=" * 70

def main():
    global CATEGORY
    global DETAIL_MAPPING
    from_company_info = CompanyInfoUtil(category=CATEGORY,
                                        detail_mapping=DETAIL_MAPPING)
    from_company_info.set_logger(get_logger(__file__))
    try:
        CompanyManagement(from_company_info).run()
    except:
        from_company_info.logger.warn('%s shuttdown' % from_company_info.category)

if __name__ == '__main__':
    main()